import {Injectable} from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { empty} from 'rxjs/observable/empty';
import { catchError, tap } from 'rxjs/operators';
import 'rxjs/add/operator/toPromise';
const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ApmService {
  constructor(private http: HttpClient, private nzMessageService: NzMessageService) {}
  private handleError () {
    return (error: any): Observable<any> => {
      const message = error.message || error.error;
      console.error('An error occurred', error);
      this.nzMessageService.remove();
      this.nzMessageService.error(message, {nzDuration: 3000});
      return empty();
    };
  }
  // 获取所有的列表
  getData(): Observable<any> {
    return this.http.get('/tourism/apm/getAll').pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
  getPageData(pageIndex, pageSize, param, type): Observable<any> {
    const pageParams = new HttpParams()
      .append('page', pageIndex)
      .append('pageSize', pageSize)
      .append('type', type)
      .append('param', param);
    return this.http.get('/tourism/apm/page', {params: pageParams}).pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
  create(data): Observable<any> {
    return this.http.post('/tourism/apm/create', JSON.stringify(data), httpOptions).pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
  update(id, data): Observable<any> {
    return this.http.put('/tourism/apm/update/' + id, JSON.stringify(data), httpOptions).pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
  delete(id): Observable<any> {
    return this.http.delete('/tourism/apm/delete/' + id).pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
  getByid(id): Observable<any> {
    return this.http.get('/tourism/apm/' + id).pipe(
      tap(response => response),
      catchError(this.handleError())
    );
  }
}

